package cn.pw.test.com.news.fragment;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.pw.test.com.news.R;
import cn.pw.test.com.news.adapter.CurrencyArticleAdapter;
import cn.pw.test.com.news.bean.CurrencyArticleBean;
import cn.pw.test.com.news.server.NewsBack;
import cn.pw.test.com.news.server.NewsGlobal;
import tech.com.commoncore.base.BaseFragment;

/**
 * @deprecated
 */
public class CurrencyArticleFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";

    private String mParam;

    private ImageView bannerImg;

    private SmartRefreshLayout smartRefresh;
    private RecyclerView contentRecycler;

    private BaseQuickAdapter articleAdapter;

    private List<CurrencyArticleBean> contentList;


    public static CurrencyArticleFragment newInstance(String param1) {
        CurrencyArticleFragment fragment = new CurrencyArticleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_currency_article;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        bannerImg = mContentView.findViewById(R.id.news_banner_img);
        smartRefresh = mContentView.findViewById(R.id.smart_refresh_layout);
        contentRecycler = mContentView.findViewById(R.id.content_recycler_view);

//        Drawable drawable = mContext.getResources().getDrawable(R.drawable.divider_recycler_vertical);
        DividerItemDecoration decoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
//        decoration.setDrawable(drawable);
        contentRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        contentRecycler.addItemDecoration(decoration);

        articleAdapter = new CurrencyArticleAdapter(R.layout.news_layout_item_article);
        contentRecycler.setAdapter(articleAdapter);

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestData(Integer.parseInt(mParam));
            }
        });

        if (mParam.equals("1")) {
            bannerImg.setImageResource(R.mipmap.news_icon_banner_2);
        } else if (mParam.equals("3")) {
            bannerImg.setImageResource(R.mipmap.news_icon_banner_3);
        } else {
            bannerImg.setVisibility(View.GONE);
        }

        if (contentList == null || contentList.isEmpty()) {
            smartRefresh.autoRefresh();
        }
    }

    private void requestData(int type) {
        NewsGlobal.getNewsImp().requestCurrencyArticle(type, 1, new NewsBack<List<CurrencyArticleBean>>() {
            @Override
            public void onBack(List<CurrencyArticleBean> beans, String err) {
                smartRefresh.finishRefresh();
                contentList = beans;
                updateView();
            }
        });
    }

    private void updateView() {
        if (contentList != null && !contentList.isEmpty()) {
            articleAdapter.replaceData(contentList);
            articleAdapter.notifyDataSetChanged();
        }
    }
}
